#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_filter)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/filter.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "filter.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

static void test_median(void)
{
    double data[] = {1, 2, 3, 2.5, 3.5, 2, 3, 4, 5};
    int size = sizeof(data) / sizeof(data[0]);
    int windowSize = 3;

    filter_median(data, size, windowSize);

    printf("Filtered data: \n");
    for (int i = 0; i < size; i++) 
    {
        printf("%f ", data[i]);
    }
    printf("\n");
}

static void test_average(void)
{
    double data[] = {1, 2, 3, 2.5, 3.5, 2, 3, 4, 5};
    int size = sizeof(data) / sizeof(data[0]);
    int windowSize = 3;
    
    filter_average(data, size, windowSize);

    printf("Filtered data: \n");
    for (int i = 0; i < size; i++) 
    {
        printf("%f ", data[i]);
    }
    printf("\n");
}

static void test_kalman(void)
{
    double measurements[] = {1.0, 2.0, 3.0, 2.5, 3.5, 2.0, 3.0, 4.0, 5.0};
    double estimates[1];
    int numMeasurements = sizeof(measurements) / sizeof(measurements[0]);
    double processNoise = 0.1;
    double measurementNoise = 0.2;
    
    filter_kalman(measurements, estimates, numMeasurements, processNoise, measurementNoise);

    printf("Estimated value: %f\n", estimates[0]);
}

static void test_base(void)
{
    test_median();
    // test_average();
    // test_kalman();
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: filter [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"                        <median>    Test median function\n"
"                        <average>   Test average function\n"
"                        <kalman>    Test kalman function\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"

    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("filter version %d.%d.%d\r\n", FILTER_V_MAJOR, FILTER_V_MINOR, FILTER_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            srand((unsigned int)time(NULL));
            #if defined(TEST_TARGET_filter)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
        else if (!strcmp(execute, "median"))
        {
            test_median();
        }
        else if (!strcmp(execute, "average"))
        {
            test_average();
        }
        else if (!strcmp(execute, "kalman"))
        {
            test_kalman();
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_filter)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_filter(void)
{
    command_export("filter", test);
}
init_export_app(test_filter);
#endif 
